package com.heima.wemedia;

import com.alibaba.fastjson.JSON;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

//基于restTemplate调用接口
public class YongyouAiDemo {
    @Test
    public void idcardTest(){
        //扫描王东镇正面照片
        Map map = idCardOcr("https://hmtt122.oss-cn-shanghai.aliyuncs.com/demo_idcard.png");
        System.out.println(map);

    }
    //扫描身份证的信息接口(身份证OCR)
    private Map idCardOcr(String idCardUrl){
        try {
            Map params = new HashMap<>();
            params.put("image",idCardUrl);
            params.put("imageType","URL");
            params.put("ocrType","0");//扫面正面设置零
            //扫描后的结果封装成map
            Map map = executePostReq("4db7e859156541499b7d5d20ada977d8", "https://api.yonyoucloud.com/apis/dst/IdcardOCR/IdcardOCR", params);
            //解析,获取success结果
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("调用接口失败");
            return null;//出现问题返回null
        }
    }

    //对外提供一个检查身份证的方法
    private Boolean idCardCheck(String idNumber,String userName){
        try {
            Map params = new HashMap<>();
            params.put("idNumber",idNumber);
            params.put("idNumber",userName);

            Map map = executePostReq("f074e0591f844f588be66e8e78960325", "https://api.yonyoucloud.com/apis/dst/matchIdentity/matchIdentity", params);
            //解析,获取success结果
            return (Boolean) map.get("success");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("调用接口失败");
            return false;
        }
    }
    /**
     * 基础的post请求(基础的接口)
     * @param apicode 用友密钥
     * @param url   要调用的接口路径
     * @param params  请求参数 map
     * @return
     */
   // ***************** 封装执行请求的方法 ********************
    private Map executePostReq(String apicode,String url,Map params){
        //创建restTemplate 客户端
        RestTemplate restTemplate = new RestTemplate();
        //封装请求头信息 apicode contentType
        HttpHeaders httpHeaders = new HttpHeaders();
        //httpHeaders.add("apicode","f074e0591f844f588be66e8e78960325");
        httpHeaders.add("apicode",apicode);
        httpHeaders.setContentType(MediaType.APPLICATION_JSON); // json
        //httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        //封装请求参数 map idNumber userName
        /*Map map = new HashMap<>();
        map.put("idNumber","210103195103222113");
        map.put("userName","王东镇");*/
        HttpEntity<String> formEntry = new HttpEntity<>(JSON.toJSONString(params), httpHeaders); // 封装请求参数
        //发起请求调用 得到返回结果
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(
                url,
                formEntry, String.class);// 发送一个post请求

        //将返回结果转为map对象
        String body = responseEntity.getBody();
        return JSON.parseObject(body, Map.class);

    }
}
